<?php
	
	error_reporting(E_ALL);
	
	class Tutorial {
	
		function Tutorial(){
			
			include($_SERVER['DOCUMENT_ROOT'] . '/include/header.class.php');
			include($_SERVER['DOCUMENT_ROOT'] . '/include/parse.class.php');
			include($_SERVER['DOCUMENT_ROOT'] . '/include/menu.class.php'); 
			include($_SERVER['DOCUMENT_ROOT'] . '/include/db.class.php'); new Mysql;
		
		}
		
		function ShowTutorial() {
			
			if(isset($_GET['t'])){
			
			
				if(strstr($_SERVER['REQUEST_URI'], '%20')){
					header( "HTTP/1.1 301 Moved Permanently" );
					header( "Location: http://www.myphpdigest.com".str_replace('%20','_',$_SERVER['REQUEST_URI']) );
				}
				
				$arr = Mysql::query('SELECT `Title`,`Article`,`Date` FROM `tutorials` WHERE `Title` = \''.str_replace('_',' ',$_GET['t']).'\'', TRUE);
				echo Header::CompileHeader($arr['Title'],substr($arr["Article"], 0, 200));
				
				// Date
				list($y,$m,$d) = split('-',$arr["Date"]);
				$arr["Date"] = date('jS M Y',mktime(0,0,0,$m,$d,$y));
				
				unset($GLOBALS['str']);
				echo Parser::Parse(array('template'=>'/template/tutorial/tutorial.tpl','bbcode'=>TRUE),
								array('{TITLE}','{DATE}','{ARTICLE}'),
								array($arr["Title"],$arr["Date"],$arr["Article"]));

				Mysql::close();
				echo Menu::CallFunctions(array($arr["Title"]));
				
			} else {

				$rr = Mysql::query('SELECT `Title`,`Description`,`Image` FROM `tutorials` ORDER BY `Date` DESC', FALSE);
				$css=1;
				
				echo Header::CompileHeader('Tutorials','Great tutorials released by John Walsh, founder of My PHP Digest, tutorials come with full explination and sometimes examples!');

				include($_SERVER['DOCUMENT_ROOT'] . '/template/tutorial/header.tpl');
				
				$quicklist = array();
				unset($GLOBALS['str']);
				
				while($arr = Mysql::fetch_array($rr)) {
					if($css>2)$css=1;
					$link = str_replace(' ','_',$arr["Title"]);
					
					
					echo Parser::Parse(array('template'=>'/template/tutorial/list.tpl','bbcode'=>FALSE),
								array('{TITLE}','{DESC}','{IMAGE}','{ID}','{URL}'),
								array($arr["Title"],$arr["Description"],$arr["Image"],$css,$link));
					$css++;
					$quicklist[] = '<a href="/tutorial/'.$link.'">'.$arr["Title"].'</a>';
				} 
			
				Mysql::close();
				echo Menu::CallFunctions($quicklist,'tutorial');
			}
			
		}

		
	}

?>